<template>
  <div class="sub-content-box" v-loading="loading">
    <div class="chart-box" v-for="item in typeList" :key="item.dictCode">
      <Line class="chart" :loading="item.loading" :chartData="item.chartData" :title="item.itemName" />
    </div>
  </div>
</template>

<script setup>
import Line from '@/components/Charts/Line/index.vue'
import { ref, onMounted, watch, computed } from 'vue'
import { getChartListData } from '@/api/common/chart'
import { getDictListByCode } from '@/api/sys/dict'

defineOptions({ name: 'ExhaustSystem' })

const props = defineProps({
  searchInfo: {
    type: Object,
    default: () => ({}),
  },
  label: {
    type: String,
    default: '',
  },
})

const typeList = ref([])
const chartList = ref([])
const dataReady = ref(false)
let dictCode = ref('')

const loading = computed(() => {
  return !dataReady.value
})

onMounted(async () => {
  formateDictCode()
  await getTypeList()
  // searchRef.value?.handleSearch()
})

// 处理字典code
const formateDictCode = () => {
  dictCode.value = props.label == '其他' ? 'energy_consumption_of_other_system' : `energy_consumption_of_${props.label.toLowerCase()}_system`
  // switch (props.label) {
  //   case 'TOX':
  //     dictCode.value = 'energy_consumption_of_tox_system'
  //     break
  //   case 'VOC':
  //     dictCode.value = 'energy_consumption_of_voc_system'
  //     break
  //   case 'AEX':
  //     dictCode.value = 'energy_consumption_of_aex_system'
  //     break
  //   case 'SCX':
  //     dictCode.value = 'energy_consumption_of_scx_system'
  //     break
  //   case 'FFU':
  //     dictCode.value = 'energy_consumption_of_ffu_system'
  //     break
  //   case 'PCW':
  //     dictCode.value = 'energy_consumption_of_pcw_system'
  //     break
  //   case 'PGEX':
  //     dictCode.value = 'energy_consumption_of_pgex_system'
  //     break
  //   case 'MAU':
  //     dictCode.value = 'energy_consumption_of_mau_system'
  //     break
  //   case 'AHU':
  //     dictCode.value = 'energy_consumption_of_ahu_system'
  //     break
  //   case 'GEX':
  //     dictCode.value = 'energy_consumption_of_gex_system'
  //     break
  //   case '其他':
  //     dictCode.value = 'energy_consumption_of_other_system'
  //     break
  //   default:
  //     dictCode.value = 'energy_consumption_of_other_system'
  //     break
  // }
}

const handleSearch = (params) => {
  if (dataReady.value) {
    getChartData(params)
  } else {
    setTimeout(() => {
      handleSearch(params)
    }, 200)
  }
}

// 监测查询参数是否变化
watch(
  () => props.searchInfo,
  (newValue) => {
    if (Object.values(newValue).length > 0) {
      handleSearch(newValue)
    }
  },
  { immediate: true },
)

const getTypeList = async () => {
  const res = await getDictListByCode(dictCode.value)
  dataReady.value = true
  typeList.value = res.map((item) => ({
    ...item,
    loading: true,
    chartData: {
      xaxisList: [],
      yaxisList: [],
    },
  }))
}

const getChartData = (searchInfo) => {
  typeList.value.forEach((item) => {
    item.loading = true
    getChartListData({
      ...searchInfo,
      dictCode: item.dictCode,
      itemNames: item.itemName,
    })
      .then((res) => {
        item.chartData = res
      })
      .finally(() => {
        item.loading = false
      })
  })
}
</script>

<style scoped lang="scss">
.sub-content-box {
  display: flex;
  flex-wrap: wrap;
  height: 100%;

  .chart-box {
    flex-shrink: 0;
    width: 50%;
    height: 50%;

    /* height: 100%; */
    padding: 4px;
  }
}
</style>
